from openai import OpenAI
from vanna.openai.openai_chat import OpenAI_Chat
from vanna.chromadb.chromadb_vector import ChromaDB_VectorStore
url,API_KEY,model_name =  "http://10.106.153.11:9002/v1",'EMPTY', 'qwen2-7b-instruct'


def single_query(query):
  return [{"role": "user", "content": query}]

def qwen_chat(client,model,history):
  # create a chat completion temperature=0.9,
  completion = client.chat.completions.create(
    model=model, max_tokens=512,
    messages=history
  )
  return completion.choices[0].message.content
# print the completion
# 用这一行测试是否能正常访问 oneapi的接口
# print(qwen_chat(client,model_name,single_query('你好')))

client = OpenAI(
    api_key=API_KEY,
    base_url=url,
)

class MyVanna(ChromaDB_VectorStore, OpenAI_Chat):
    def __init__(self, client=None, config=None):
        ChromaDB_VectorStore.__init__(self, config=config)
        OpenAI_Chat.__init__(self, client=client, config=config)

vn = MyVanna(client=client, config={"model": model_name})
#vn.max_tokens = 1000
vn.temperature = 1
vn.train(ddl="""
CREATE TABLE IF NOT EXISTS users (
    id INT PRIMARY KEY COMMENT '用户ID',
    username VARCHAR(50) COMMENT '用户名',
    email VARCHAR(100) COMMENT '电子邮件',
    age INT COMMENT '年龄',
    gender VARCHAR(10) COMMENT '性别（男/女）',
    city VARCHAR(50) COMMENT '城市'
) COMMENT='用户信息表' CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
""")
vn.train(ddl="CREATE TABLE IF NOT EXISTS consumption_record (   id INT PRIMARY KEY COMMENT '消费记录ID',"
             "user_id COMMENT '用户id' ,item_id INT COMMENT '商品id',amount INT COMMENT '数量' ,consumption INT COMMENT '总消费'"
             ") COMMENT='购买记录' CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;  ")
query = '男性用户的总消费是多少'
res = vn.generate_sql(query)
print("#"*10)
print(query,res)
print("#"*10)
query = '男性用户有多少个'
res = vn.generate_sql('男性用户有多少个')

print("#"*10)
print(query,res)
print("#"*10)
